package Integer;

/**
 * Some usefull methods for handling integer numbers
 * 
 * @author Torbjoern Klatt
 */
public class Numbers {
    /**
     * Calculating the sum of digits of an integer number.
     * @param number as Integer
     * @return int
     */
    public static int sumOfDigits(int number) {
        int rest = number, result = 0;
        while(rest > 0) {
            number = rest % 10;
            rest = rest / 10;
            result += number;
        }
        return result;
    }
    
    /**
     * Returns the length of an integer number.
     * @param number as Integer
     * @return int
     */
    public static int getIntLength(int number) {
        int rest = number, n = -1;
        while(rest > 0) {
            number = rest % 10;
            rest = rest / 10;
            n++;
        }
        return n;
    }
    
    /**
     * Parsing an Integer number figure by figure into an integer array. This
     * method uses the method getIntLength of this class.
     * @param number as Integer
     * @return int[]
     */
    public static int[] parseIntoArray(int number) {
        int rest = number, backup = number, n = Numbers.getIntLength(number);
        int[] figures = new int[n+1];
        rest = backup;
        for(int i = n; i >= 0; i--) {
            figures[i] = rest % 10;
            rest = rest / 10;
        }
        return figures;
    }
    
    /**
     * Calculating the sum of all numbers from a given lower bound to a given
     * upper bound.
     * @param n as Integer
     * @param m as Integer
     * @return int
     */
    public static int sumFromTo(int n, int m) {
        int result = 0;
        for(int i = n; i <= m; i++) {
            result += i;
        }
        return result;
    }
    
    /**
     * Calculating the power of a given number.
     * @param n as Integer
     * @return Double
     */
    public static double fakultaet(int n) {
        double result = 1;
        for(int i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }
    
    /**
     * Calculating the binominal koefficient of two given numbers.
     * @param n as Integer
     * @param k as Integer
     * @return Integer
     */
    public static int binominal(int n, int k) {
        double result;
        result = Numbers.fakultaet(n) / (Numbers.fakultaet(k) * Numbers.fakultaet(n-k));
        return (int) result;
    }
}
